package com.ews.onlineexchange.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.ews.onlineexchange.model.Attachment;
import com.ews.onlineexchange.model.approved.Evidence;
import com.ews.onlineexchange.model.approved.EvidenceDefination;
import com.ews.onlineexchange.service.ViolationRecordHome;
import com.ews.onlineexchange.util.AttachmentUtils;

import antlr.debug.Event;

@Named
@ConversationScoped
public class ViolationRecordController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	ViolationRecordHome violationRecordHome;
	
	private Map<String, List<Attachment>> uploadMap = new HashMap<>();
	
	public void uploadEvidence(FileUploadEvent event) {
		String clientId = event.getComponent().getClientId();
		String index = ViolationRecordController.getIteratorClientIndex(clientId);
		List<Attachment> attrList = getUploadMap().get(index);
		if (attrList == null) {
			attrList = new ArrayList<Attachment>();
			getUploadMap().put(index, attrList);
		}
		Attachment attr = getAttachment(event.getFile());
		attrList.add(attr);
		FacesMessage message = new FacesMessage(event.getFile().getFileName() + " 上传成功。");
        FacesContext.getCurrentInstance().addMessage(null, message);
	}
	@Inject
	AttachmentUtils attachmentUtils;
	private Attachment getAttachment(UploadedFile file) {
		Attachment attr = attachmentUtils.getAttachmentFromUpload(file);
		return attr;
	}

	public ViolationRecordHome getViolationRecordHome() {
		return violationRecordHome;
	}

	public void setViolationRecordHome(ViolationRecordHome violationRecordHome) {
		this.violationRecordHome = violationRecordHome;
	}
	
	private static String getIteratorClientIndex(String clientId) {
		clientId = clientId.substring(0, clientId.lastIndexOf(":"));
		clientId = clientId.substring(clientId.lastIndexOf(":") + 1, clientId.length());
		return clientId;
	}

	public Map<String, List<Attachment>> getUploadMap() {
		return uploadMap;
	}
	
	public String save() {
		if (!validateEvidences()) {
			return null;
		}
		
		generateEvidences();
		
		return violationRecordHome.saveAndFlush();
	}

	private void generateEvidences() {		
		Integer i = 0;
		for (Iterator<EvidenceDefination> iterator = violationRecordHome.getInstance().getApprovedMatter().getEvidenceDefinations().iterator(); iterator.hasNext();) {
			EvidenceDefination _def = (EvidenceDefination) iterator.next();
			Evidence evidence = new Evidence();
			evidence.setAttachments(getUploadMap().get(i.toString()));
			evidence.setDefination(_def);
			violationRecordHome.getInstance().getEvidences().add(evidence);
			i++;
		}
	}

	private boolean validateEvidences() {
		Integer i = 0;
		boolean hasError = false;
		for (Iterator<EvidenceDefination> iterator = violationRecordHome.getInstance().getApprovedMatter().getEvidenceDefinations().iterator(); iterator.hasNext();) {
			EvidenceDefination _def = (EvidenceDefination) iterator.next();
			List<Attachment> uploadedFiles = uploadMap.get(i.toString());
			if (_def.isRequired() && (uploadedFiles == null || uploadedFiles.isEmpty())) {
				FacesMessage message = new FacesMessage(_def.getName() + "必须上传。");
		        FacesContext.getCurrentInstance().addMessage(null, message);
		        hasError = true;
			}
			i++;
		}
		if (hasError)
			return false;
		return true;
	}

}
